directives.directive('sheet', ["$rootScope", "tools", 'tmp', "$timeout", "$routeParams", "sheetServ", function ($rootScope, tools, tmp, $timeout, $routeParams, sheetServ) {
    return {
        restrict: 'E',
        scope: true,
        replace: true,
        link: function (scope, element, attr) {
            var config = scope.config = (scope.getSheet ? scope.getSheet : $rootScope.getSheet)(attr.moduleid ? Number(attr.moduleid) : Number(attr.tmpid));
            var $container = $('.charts-container');
            var base = attr.base ? Number(attr.base) : $container.width();
            var $ele = $(element);
            var option = null;
            // config.filters = {
            //     sortField: '',
            //     sortType: ''
            // };
            if(!config.filters){
                config.filters = {
                    sortField:'',
                    sortType:''
                }
            }
            var events = {};

            scope.previewing = attr.previewing == '1' ? true : false;
            scope.maxable = attr.maxable == '1' ? true : false;
            scope.loading = false;

            if (attr.left) {
                config.left = Number(attr.left);
            }
            if (attr.top) {
                config.top = Number(attr.top);
            }
            if (attr.width) {
                config.width = Number(attr.width);
            }
            if (attr.height) {
                config.height = Number(attr.height);
            }

            // 非移动端模式下才是绝对定位,移动端下增加一个底部margin
            if (!config.mobileMode) {
                $ele.addClass('pa');
            } else {
                $ele.addClass('mb20');
                $ele.addClass('pr');
            }

            var menus = [
                {
                    name: '修改标题',
                    icon: 'write icon',
                    hide: function () {
                        return !config.moduleId;
                    },
                    fn: function () {
                        if (!config.moduleId) {
                            return;
                        }
                        $rootScope.$broadcast('INPUT_EDIT_EV', config.ctitle, function (content) {
                            config.ctitle = content;
                        });
                        $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                    }
                },
                {
                    name: '复制',
                    hide: function () {
                        return !config.moduleId;
                    },
                    icon: 'paste icon'
                },
                // {
                //     name: '转化为图',
                //     icon: "icon table",
                //     hide: function () {
                //         if (config.tabType == 'crosstab' || config.tabType == 'detailtab' || !config.transformChartToTable) {
                //             return true;
                //         }
                //         if (!config.moduleId) {
                //             return true;
                //         }
                //     },
                //     fn: function () {
                //         // 删除chart
                //         sheetServ.deleteIbmModuleDataByModuleId(config, attr.type);
                //         // 添加新的表
                //         $rootScope.$broadcast('ADD_CHART_EV', config.originChartType || 'StandardPieChart', {
                //             left: $ele.position().left / base,
                //             top: $ele.position().top / base,
                //             height: $ele.height() / base,
                //             width: $ele.width() / base,
                //             transformSheetToChart: true,
                //             measure: angular.copy(config.dataFieldNames),
                //             dimension: angular.copy(config.lineFieldNames)
                //         });
                //     }
                // },
                {
                    name: '下载为',
                    icon: 'download icon',
                    hide: function () {
                        return !config.moduleId;
                    },
                    submenus: [
                        {
                            name: 'JPEG图片',
                            icon: 'file image outline icon'
                        },
                        {
                            name: 'png图片',
                            icon: 'file image outline icon'
                        }
                    ]
                },
                {
                    name: '清空',
                    icon: 'erase icon',
                    hide: function () {
                        return !config.moduleId;
                    },
                    fn: function () {
                        if (config.moduleId) {
                            $rootScope.$broadcast('CONFIRM_EV', {
                                title: '图清除提醒',
                                content: '图清除之后不可恢复,确定清除吗?',
                                btns: {
                                    yes: {
                                        text: "确定删除",
                                        fn: function () {
                                            config.lineFieldNames.length = config.listFieldNames.length = config.dataFieldNames.length = 0;
                                            scope.recover_fields(false);
                                            addIbmModuleTable();
                                            config.ctitle = "";
                                        }
                                    },
                                    no: {
                                        text: '取消',
                                        fn: function () {

                                        }
                                    }
                                }
                            });
                        } else {
                            config.lineFieldNames.length = config.listFieldNames.length = config.dataFieldNames.length = 0;
                            scope.recover_fields(false);
                            addIbmModuleTable();
                            config.ctitle = "";
                        }
                    }
                },
                {
                    name: '删除',
                    icon: 'trash icon',
                    fn: function () {
                        if (config.moduleId) {
                            $rootScope.$broadcast('CONFIRM_EV', {
                                title: '图删除提醒',
                                content: '图表删除之后不可恢复,确定删除吗?',
                                btns: {
                                    yes: {
                                        text: "确定删除",
                                        fn: function () {
                                            sheetServ.deleteIbmModuleDataByModuleId(config, attr.type);
                                        }
                                    },
                                    no: {
                                        text: '取消'
                                    }
                                }
                            });
                        } else {
                            $rootScope.delSheet(config.tmpId);
                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                            $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                            $rootScope.$broadcast('FIELD_RECOVER_FROM_SHEET_EV', [], [], []);
                        }
                    }
                }
            ];

            // var hideMenuItems = function (hiddens) {
            //     angular.forEach(menus, function (item) {
            //         if (hiddens.indexOf(item.name) != -1) {
            //             item.hide = true;
            //         }
            //     });
            // }

            // 只有汇总表可以转换为


            $ele.on('contextmenu', function (e) {
                e.stopPropagation();
                if (e.button == 2) {
                    $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, menus);
                }
                return false;
            });

            scope.recover_fields = function (flag) {
                if (flag) {
                    $rootScope.$broadcast('FIELD_RECOVER_FROM_SHEET_EV', config.lineFieldNames, config.listFieldNames, config.dataFieldNames, attr.type,config.intervalPeriodUnit);
                    $rootScope.$broadcast('SELECTED_TYPE_EV', scope.config.tabType);
                } else {
                    $rootScope.$broadcast('FIELD_RECOVER_FROM_SHEET_EV', [], [], [], attr.type,config.intervalPeriodUnit);
                    $rootScope.$broadcast('SELECTED_TYPE_EV', scope.config.tabType);
                }
            }

            scope.fullScreen = function () {
                $rootScope.$broadcast('SHEET_FULL_SCREEN_EV', config);
            }

            // 初始化
            $ele.css({
                width: config.width <= 1 ? base * config.width + 'px' : config.width + 'px',
                height: config.height <= 1 ? base * config.height + 'px' : config.height + 'px'
            });

            // 初始化
            if (!tools.mobileEnv()) {
                // $ele.css({
                //     top: config.top <= 1 ? base * config.top + 'px' : config.top + 'px',
                //     left: config.left <= 1 ? base * config.left + 'px' : config.left + 'px'
                // });
                $ele.css({
                    top: base * config.top + 'px',
                    left: base * config.left + 'px'
                });
            }

            $ele.on('click', function (e) {
                e.stopPropagation();
            });

            if (base * config.top + $ele.height() > $container.height() - 100) {
                $container.height($container.height() + 150);
            }

            // 如果下方空间不够则扩展
            if (base * config.top + $ele.height() > $container.height() - 100) {
                $container.height($container.height() + 150);
            }

            if (config.moduleId) {
                // if(config.sortType){
                //     filters = {
                //         sortType:config.sortType,
                //         sortField:config.sortField
                //     }
                // }
                getDatasByModuleId(0, 10);
            } else {
                $ele.addClass(attr.type + ' border');
            }

            // 如果来自于由图转化为表
            if (config.transformChartToTable) {
                addIbmModuleTable();
            }

            // 监听主排序事件
            events.MAIN_SORT_EV_OFF = $rootScope.$on('MAIN_SORT_EV', function (e, sortField, sortType) {
                if (scope.selected) {
                    // filters.sortField = sortField;
                    // filters.sortType = sortType;
                    config.sortField = sortField;
                    config.sortType = sortType;
                    config.filters = {
                        sortType: sortType,
                        sortField: sortField
                    }

                    getDatasByModuleId(0, 10);
                    
                }
            });
            // 监听度量变化事件
            events.MEA_CHANGE_EV_OFF = $rootScope.$on('MEA_CHANGE_EV', function (e, fieldId, meaType) {
                if (scope.selected) {
                    angular.forEach(config.dataFieldNames, function (dim) {
                        if (dim.fieldId == fieldId) {
                            dim.meaType = meaType;
                        }
                    });
                    addIbmModuleTable();
                }
            });

            //监听留存率周期增加事件
            events.INTERVALPERIODUNIT_ADD_EV_OFF = $rootScope.$on('INTERVALPERIODUNIT_ADD_EV', function (e, intervalPeriodUnit) {
                if (scope.selected) {
                    config.intervalPeriodUnit = intervalPeriodUnit;
                    addIbmModuleTable();
                }
            });

            // 监听横坐标增加
            events.DIMENSION_ADD_EV_OFF = $rootScope.$on('DIMENSION_ADD_EV', function (e, dimension) {
                if (scope.selected) {
                    config.lineFieldNames.push(dimension);
                    addIbmModuleTable();
                    // if(config.tabType == 'retentionrate'){
                    //     config.lineFieldNames.length = 0;
                    //     config.lineFieldNames.push(dimension);
                    //     addIbmModuleTable();
                    // } else {
                    //     config.lineFieldNames.push(dimension);
                    //     addIbmModuleTable();
                    // }
                    
                    
                }
            });

            // 监听横坐标减少
            events.DIMENSION_REDUCE_EV_OFF = $rootScope.$on('DIMENSION_REDUCE_EV', function (e, columnId) {
                if (scope.selected) {
                    angular.forEach(config.lineFieldNames, function (dim, index) {
                        if (dim.columnId == columnId) {
                            config.lineFieldNames.splice(index, 1);
                            addIbmModuleTable();
                        }
                    });

                    if (config.chartType == 'summarytab' && config.dimension.length == 0 && config.measure.length == 0) {
                        config.ctitle = "";
                    }
                    if (config.chartType == 'crosstab' && (config.lineFieldNames.length == 0 || config.listFieldNames.length == 0 || config.dataFieldNames.length == 0)) {
                        config.ctitle = "";
                    }
                }
            });

            // 监听横坐标覆盖事件
            events.DIMENSION_RECOVER_EV_OFF = $rootScope.$on('DIMENSION_RECOVER_EV', function (e, lineFieldNames) {
                if (scope.selected) {
                    config.lineFieldNames = angular.copy(lineFieldNames);
                    addIbmModuleTable();
                }
            });

            // 监听第二横坐标增加
            events.DIMENSION_2_ADD_EV_OFF = $rootScope.$on('DIMENSION2_ADD_EV', function (e, dimension) {
                if (scope.selected) {
                    config.listFieldNames.push(dimension);
                    addIbmModuleTable();
                }
            });

            // 监听第二横坐标减少
            events.DIMENSION_2_REDUCE_EV_OFF = $rootScope.$on('DIMENSION2_REDUCE_EV', function (e, columnId) {
                if (scope.selected) {
                    angular.forEach(config.listFieldNames, function (dim, index) {
                        if (dim.columnId == columnId) {
                            config.listFieldNames.splice(index, 1);
                            addIbmModuleTable();
                        }
                    });

                    if (config.chartType == 'summarytab' && config.dimension.length == 0 && config.measure.length == 0) {
                        config.ctitle = "";
                    }
                    if (config.chartType == 'crosstab' && (config.lineFieldNames.length == 0 || config.listFieldNames.length == 0 || config.dataFieldNames.length == 0)) {
                        config.ctitle = "";
                    }
                }
            });

            // 监听第二横坐标覆盖事件
            events.DIMENSION2_RECOVER_EV_OFF = $rootScope.$on('DIMENSION2_RECOVER_EV', function (e, listFieldNames) {
                if (scope.selected) {
                    config.listFieldNames = angular.copy(listFieldNames);
                    addIbmModuleTable();
                }
            });

            // 监听纵坐标增加
            events.MEASURE_ADD_EV_OFF = $rootScope.$on('MEASURE_ADD_EV', function (e, measure) {
                if (scope.selected) {
                    if (config.tabType != 'crosstab') {
                        config.dataFieldNames.push(measure);
                    } else {
                        config.dataFieldNames.length = 0;
                        config.dataFieldNames.push(measure);
                    }
                    $timeout(function () {

                    }, 0);
                    addIbmModuleTable();
                }
            });

            // 监听纵坐标减少
            events.MEASURE_REDUCE_EV_OFF = $rootScope.$on('MEASURE_REDUCE_EV', function (e, columnId) {
                if (scope.selected) {
                    angular.forEach(config.dataFieldNames, function (mea, index) {
                        if (mea.columnId == columnId) {
                            config.dataFieldNames.splice(index, 1);
                            addIbmModuleTable();
                        }
                    });

                    if (config.tabType == 'summarytab' && (config.lineFieldNames.length == 0 || config.dataFieldNames.length == 0)) {
                        $timeout(function () {
                            config.ctitle = "";
                        }, 0);
                    }
                    if (config.chartType == 'crosstab' && (config.lineFieldNames.length == 0 || config.listFieldNames.length == 0 || config.dataFieldNames.length == 0)) {
                        config.ctitle = "";
                    }
                }
            });

            // 监听纵坐标覆盖事件
            events.MEASUREMENT_RECOVER_EV_OFF = $rootScope.$on('MEASUREMENT_RECOVER_EV', function (e, measures) {
                if (scope.selected) {
                    config.dataFieldNames = angular.copy(measures);
                    addIbmModuleTable();
                }
            });

            // 增加模块
            function addIbmModuleTable () {
                var dataFieldNames = [];
                var listFieldNames = [];
                var lineFieldNames = [];

                if (config.tabType == 'summarytab') {
                    if (config.lineFieldNames.length === 0 || config.dataFieldNames.length === 0) {
                        scope.datas.length = 0;
                        $ele.addClass(attr.type);
                        return;
                    }
                } else if (config.tabType == 'crosstab') {
                    if (config.lineFieldNames.length == 0 || config.listFieldNames.length == 0 || config.dataFieldNames.length == 0) {
                        scope.datas.length = 0;
                        $ele.addClass(attr.type);
                        return;
                    }
                } else if (config.tabType == 'detailtab') {
                    if (config.lineFieldNames.length === 0) {
                        scope.datas.length = 0;
                        $ele.addClass(attr.type);
                        return;
                    }
                } else if (config.tabType == 'retentionrate') {
                    if (config.lineFieldNames.length === 0 || config.listFieldNames.length == 0 || config.intervalPeriodUnit.val== '') {
                        scope.datas.length = 0;
                        $ele.addClass(attr.type);
                        return;
                    }
                }

                angular.forEach(config.lineFieldNames, function (dim) {
                    if (config.tabType != 'detailtab' && config.tabType != 'retentionrate') {
                        lineFieldNames.push(dim.meaType + '_' + dim.fieldName);
                    } else {
                        lineFieldNames.push(dim.fieldName);
                    }
                });
                angular.forEach(config.listFieldNames, function (lis) {
                    if (config.tabType != 'detailtab' && config.tabType != 'retentionrate') {
                        listFieldNames.push(lis.meaType + '_' + lis.fieldName);
                    } else {
                        listFieldNames.push(lis.fieldName);
                    }
                });
                angular.forEach(config.dataFieldNames, function (mea) {
                    if (config.tabType != 'detailtab') {
                        dataFieldNames.push(mea.meaType + '_' + mea.fieldName);
                    } else {
                        dataFieldNames.push(mea.fieldName);
                    }
                });

                scope.loading = true;
                var data = {};
                if(config.tabType == 'retentionrate') {
                    data = {
                        tableType: $routeParams.tableType,
                        pageId: $routeParams.id,
                        tbId: $routeParams.tbId,
                        tabType: attr.type,
                        intervalPeriodUnit : config.intervalPeriodUnit.val,
                        dateDimensionFieldName : lineFieldNames.join(','),
                        retentionMarkFieldName : listFieldNames.join(',')

                    }
                } else {
                    data = {
                        lineFieldNames: lineFieldNames.join(','),
                        dataFieldNames: dataFieldNames.join(','),
                        listFieldNames: listFieldNames.join(','),
                        tableType: $routeParams.tableType,
                        pageId: $routeParams.id,
                        tbId: $routeParams.tbId,
                        tabType: attr.type,
                        chartId: attr.type,

                    }
                }

                tools.get({
                    url: '/Ibm-Client/addIbmModuleTable.htm',
                    data: data,
                    succ: function (resp) {
                        if (resp.success) {
                            config.moduleId = resp.value;
                            getDatasByModuleId(0, 10);

                            if (!attr.moduleid) {
                                // tmp.data.sheets.push(config);
                                attr.moduleid = config.moduleId;
                            }
                        }
                    }
                });
            }

            // 获取数据
            var lineHeaderSize, listHeaderSize;

            function getDatasByModuleId (beginIndex, resultSize) {
                var data = {
                    pageId: $routeParams.id,
                    moduleId: config.moduleId,
                    beginIndex: 0,
                    resultSize: 500,

                    /*beginIndex: config.tabType == 'crosstab' ? 0 : beginIndex,
                     resultSize: config.tabType == 'crosstab' ? 1000 : resultSize*/
                };

                //angular.extend(data, filters);

                //config.filters.downDrillValue = config.downDrillValues.join(',');
                angular.extend(data, config.filters);

                scope.loading = true;

                tools.get({
                    url: "/Ibm-Client/getDatasByModuleId.htm",
                    data: data,
                    succ: function (resp) {
                        if (resp.success) {
                            lineHeaderSize = scope.lineHeaderSize = resp.value.lineHeaderSize;
                            listHeaderSize = scope.listHeaderSize = resp.value.listHeaderSize;

                            if (resp.value.totalPages) {
                                $timeout(function () {
                                    scope.totalPages = resp.value.totalPages;
                                }, 0);
                            }
                            scope.drawSheet(option = resp.value.datas, resp.value.totalPages);
                            $ele.removeClass(attr.type);

                            $timeout(function () {
                                scope.loading = false;
                            }, 0);
                        } else {
                            scope.loading = false;
                            tools.msg('系统繁忙,请稍后再试!');
                        }
                        // scope.recover_fields(true);
                    }
                });
            }

            scope.pageChanged = function (newPageNumber) {
                getDatasByModuleId((newPageNumber - 1) * 10, 10);
            }

            scope.stopPropagation = function (e) {
                e.stopPropagation();
            }

            // 绘制数据
            scope.table_shown = false;
            scope.datas = [];

            scope.drawSheet = function (datas, totalPages) {
                angular.forEach(datas, function (line) {
                    angular.forEach(line, function (data, index, arr) {
                        if (data === '') {
                            arr[index] = '';
                        }
                    });
                });
                $timeout(function(){
                    scope.datas = datas;
                    scope.datas.paginationId = Math.random() * Math.random();
                    scope.table_shown = true;
                    $(element).find('.table-container').scrollTop(0)
                },0)
                

                $timeout(function () {
                    if (totalPages instanceof Number) {
                        scope.totalPages = totalPages;
                    }


                    // 在滚动的时候固定左边和上边的表头
                    headerFix();

                }, 50);
            }

            // 保存
            events.SAVE_EV_OFF = $rootScope.$on('SAVE_EV', function () {
                var base = $container.width();

                // if (config.width <= 1) {
                //     config.width = $ele.width() / base;
                // }
                // if (config.height <= 1) {
                //     config.height = $ele.height() / base;
                // }
                // if (config.top <= 1) {
                //     config.top = $ele.position().top / base;
                // }
                // if (config.left <= 1) {
                //     config.left = $ele.position().left / base;
                // }

                config.width = $ele.width() / base;
                config.height = $ele.height() / base;
                config.top = $ele.position().top / base;
                config.left = $ele.position().left / base;

                config.selected = false;

                angular.forEach(config.lineFieldNames, function (dim) {
                    for (var i in dim) {
                        if (dim[i] === null) {
                            dim[i] = "";
                        }
                    }
                });
                angular.forEach(config.listFieldNames, function (mea) {
                    for (var i in mea) {
                        if (mea[i] === null) {
                            mea[i] = "";
                        }
                    }
                });
                angular.forEach(config.dataFieldNames, function (mea) {
                    for (var i in mea) {
                        if (mea[i] === null) {
                            mea[i] = "";
                        }
                    }
                });
            });

            // 监听尺寸变动事件
            events.RESIZE_EV_OFF = $rootScope.$on('RESIZE_EV', function (e) {
                if (!scope.selected)
                    return;

                headerFix();
            });

            // 筛选组件传来筛选条件
            events.FILTER_EV_OFF = $rootScope.$on('FILTER_EV', function (e, conditionId, value) {
                if (value) {
                    config.filters['condition-' + conditionId] = value;
                } else {
                    config.filters['condition-' + conditionId] = null;
                }
                getDatasByModuleId(0, 10);
            });

            // 清除筛选条件的事件
            events.FILTER_CLEAR_EV_OFF = $rootScope.$on('FILTER_CLEAR_EV', function (e, conditionId) {
                if (!conditionId)
                    return;

                for (var i in config.filters) {
                    if (i.indexOf(conditionId) != -1) {
                        delete config.filters[i];
                    }
                }

                getDatasByModuleId(0, 10);
            });

            // 监听到联动事件
            events.LINK_EV_OFF = $rootScope.$on('LINK_EV', function (e, link) {
                if (link.id == config.moduleId) {
                    return;
                }
                if (!link.dimensionValue) {
                    config.filters = {
                        sortField: '',
                        sortType: ''
                    };
                    getDatasByModuleId(0, 10);
                    return;
                }
                angular.extend(config.filters, link);
                getDatasByModuleId(0, 10);
            });

            // 监听取消预览事件
            events.PREVIEW_OFF_EV_OFF = $rootScope.$on('PREVIEW_OFF_EV', function () {
                $(element).find('.table-for-list-header table');
                $(element).find('.table-for-line-header table');
                $(element).find('.table-for-corn-header table');
            });

            // 删除表
            events.DEL_SHEET_EV_OFF = $rootScope.$on('DEL_SHEET_EV', function (e) {
                sheetServ.deleteIbmModuleDataByModuleId(config, attr.type);
            });

            // 更新sheet,因为左侧字段列表的重命名
            events.UPDATE_SHEET_EV_OFF = $rootScope.$on('UPDATE_SHEET_EV', function (e, columnId, columnDesc) {
                angular.forEach(config.lineFieldNames, function (line) {
                    if (line.columnId === columnId) {
                        line.columnDesc = columnDesc;
                        config.ctitle = "";
                        addIbmModuleTable();
                        // getDatasByModuleId(0, 10);
                    }
                });
                angular.forEach(config.listFieldNames, function (list) {
                    if (list.columnId === columnId) {
                        list.columnDesc = columnDesc;
                        config.ctitle = "";
                        addIbmModuleTable();
                        // getDatasByModuleId(0, 10);
                    }
                });
                angular.forEach(config.dataFieldNames, function (data) {
                    if (data.columnId === columnId) {
                        data.columnDesc = columnDesc;
                        config.ctitle = "";
                        addIbmModuleTable();
                        // getDatasByModuleId(0, 10);
                    }
                });
            });

            scope.$on('$destroy', function () {
                for (var i in events) {
                    if (i.indexOf('_OFF') != -1) {
                        events[i]();
                    }
                }
            });

            // 自适应大小和滚动条
            var scrollTop, scrollLeft;

            function headerFix () {
                $(element).find('.table-for-data table tr').each(function (index, ele) {
                    $(element).find('.table-for-list-header table tr:eq(' + index + ')').css('height', $(ele).height() + 'px');
                    $(element).find('.table-for-line-header table tr:eq(' + index + ')').css('height', $(ele).height() + 'px');
                    $(element).find('.table-for-corn-header table tr:eq(' + index + ')').css('height', $(ele).height() + 'px');
                });

                var table_for_data_trs = $(element).find('.table-for-data table tr');
                var table_for_list_trs = $(element).find('.table-for-list-header table tr');
                var table_for_line_trs = $(element).find('.table-for-line-header table tr');
                var table_for_corn_trs = $(element).find('.table-for-corn-header table tr');

                for (var i = 0; i < lineHeaderSize; i++) {
                    var data_tds = table_for_data_trs.eq(i).find('td');
                    var line_tds = table_for_line_trs.eq(i).find('td');

                    // 固定行表头的宽度可能会因为数据太宽而导致与行表头宽度不一致,在此统一
                    for (var j = 0; j < data_tds.size(); j++) {
                        if (line_tds.eq(j).css('width') != data_tds.eq(j).css('width')) {
                            line_tds.eq(j).css('width', data_tds.eq(j).css('width'));
                        }
                    }
                }

                for (var j = 0; j < listHeaderSize; j++) {
                    for (var k = 0; k < scope.datas.length; k++) {
                        var td0 = table_for_list_trs.eq(k).find('td').eq(j);
                        var td1 = table_for_data_trs.eq(k).find('td').eq(j);
                        td0.css('width', td1.css('width'));
                    }
                }

                for (var j = 0; j < listHeaderSize; j++) {
                    for (var k = 0; k < lineHeaderSize; k++) {
                        var td0 = table_for_corn_trs.eq(k).find('td').eq(j);
                        var td1 = table_for_data_trs.eq(k).find('td').eq(j);
                        td0.css('width', td1.css('width'));
                    }
                }

                $(element).find('.table-container').scroll(function () {
                    if ($(this).scrollLeft() != scrollLeft) {
                        scrollLeft = $(this).scrollLeft();
                        if (scrollLeft > 30) {
                            $(element).find('.table-for-list-header').css({
                                left: scrollLeft + 'px',
                                zIndex: 10
                            }).removeClass('none');
                            $(element).find(".table-for-line-header").css({
                                zIndex: 2
                            });
                        } else {
                            $(element).find('.table-for-list-header').addClass('none');
                            $(element).find('.table-for-corn-header').addClass('none');
                        }
                    } else {
                        scrollTop = $(this).scrollTop();
                        if (scrollTop > 30) {
                            $(element).find(".table-for-line-header").css({
                                top: scrollTop + 'px',
                                zIndex: 10
                            }).removeClass('none');
                            $(element).find('.table-for-list-header').css({
                                zIndex: 2
                            });
                        } else {
                            $(element).find(".table-for-line-header").addClass('none');
                            $(element).find('.table-for-corn-header').addClass('none');
                        }
                    }

                    // 如果纵表头和横表头都显示则左上角固定表头也显示出来
                    if (scrollTop > 30 && scrollLeft > 30) {
                        $(element).find('.table-for-corn-header').css({
                            left: scrollLeft + 'px',
                            top: scrollTop + 'px'
                        }).removeClass('none');
                    }
                });

                // 检测时候该滚动
                if ($ele.find('.sheet-wrap table').width() > $ele.find('.sheet-wrap').width()) {
                    $(element).find('.sheet-wrap').css('overflow-x', 'scroll');
                } else {
                    $(element).find('.sheet-wrap').css('overflow-x', 'auto');
                }

                if ($ele.find('.sheet-wrap table').height() > $ele.find('.sheet-wrap').height()) {
                    $(element).find('.sheet-wrap').css('overflow-y', 'scroll');
                } else {
                    $(element).find('.sheet-wrap').css('overflow-y', 'auto');
                }
            }

            scope.fullscreen = function () {
                $rootScope.$broadcast('FULL_SCREEN_EV', "sheet", sheetlist[moduleId]);
            }

            scope.mobileEnv = tools.mobileEnv;
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/sheet/sheetView.html"
    }
}]);
services.factory('sheetServ', ["$rootScope", "tools", "$timeout", "$routeParams", function ($rootScope, tools, $timeout, $routeParams) {
    return {
        deleteIbmModuleDataByModuleId: function (config, type, cb) {
            if (!config) {
                return;
            }
            if (!config.moduleId && config.tmpId) {
                return $rootScope.delSheet(config.tmpId);
            }
            tools.get({
                url: '/Ibm-Client/deleteIbmModuleDataByModuleId.htm',
                data: {
                    moduleId: config.moduleId,
                    pageId: $routeParams.id
                },
                succ: function (resp) {
                    if (resp.success) {
                        cb && cb(resp);
                        $rootScope.delSheet(config.tmpId || config.moduleId);

                        $rootScope.$broadcast('CONFIRM_OFF_EV');
                        $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                        $rootScope.$broadcast('FIELD_RECOVER_FROM_SHEET_EV', [], [], [], type);
                    }
                }
            });
        }
    }
}]);